/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#ifndef LOOKUPLINEEDIT_H
#define LOOKUPLINEEDIT_H

#include <QLineEdit>
#include <QModelIndex>
#include <QItemSelection>
#include <QSqlRecord>
#include "win32def.h"

/**
	@author BogDan Vatra <taipan@licentia.ro>
*/
class Validator;
class QCompleter;
class InfoView;

namespace SQL
{class Query;};

class KONTAMABIL_EXPORTS LookupLineEdit : public QLineEdit
{
	Q_OBJECT
	Q_PROPERTY(bool handleFocus READ handleFocus WRITE setHandleFocus)
	Q_PROPERTY(QString listQuery READ listQuery WRITE setListQuery)
	Q_PROPERTY(QString listField READ listField WRITE setListField)
	Q_PROPERTY(bool sortedListField READ isSortedListField WRITE setSortedListField)
	Q_PROPERTY(QStringList infoFields READ infoFields WRITE setInfoFields)
	Q_PROPERTY(QStringList infoHeads READ infoHeads WRITE setInfoHeads)
	Q_PROPERTY(QString keyField READ keyField WRITE setKeyField)
	Q_PROPERTY(QString appendPluginName READ appendPluginName WRITE setAppendPluginName)
	Q_PROPERTY(QString dataQuery READ dataQuery WRITE setDataQuery)
	Q_PROPERTY(QString dataField READ dataField WRITE setDataField)
	Q_PROPERTY(bool validateInput READ validateInput WRITE setValidateInput)

public:
	LookupLineEdit(QWidget *parent = 0);
	~LookupLineEdit();

	QString listQuery();
	void setListQuery(const QString & listQuery);

	QString listField();
	void setListField(const QString & listField);

	bool isSortedListField();
	void setSortedListField(bool sortedListField);

	QStringList infoFields();
	void setInfoFields(const QStringList & infoFields);

	QStringList infoHeads();
	void setInfoHeads(const QStringList & infoHeads);;

	QString keyField();
	void setKeyField(const QString & keyField);

	QString appendPluginName();
	void setAppendPluginName(const QString & appendPluginName);

	QString dataQuery();
	void setDataQuery(const QString & dataQuery);

	QString dataField();
	void setDataField(const QString & dataField);

	void setValidateInput(bool validate);
	bool validateInput();

	bool handleFocus();
	void setHandleFocus(bool handleFocus);
signals:
	void escapePressed();
	void focusIn();
	void focusOut();

protected:
	void keyPressEvent(QKeyEvent * event);
	void showEvent(QShowEvent * event);
	void focusInEvent(QFocusEvent * event);
	void focusOutEvent(QFocusEvent * event);

private slots:
	void recordChanged();
	void setReadWrite();
	void setReadOnly();
	void bindValue();
	void setTextToEdit();
	void listRecordChanged(const QSqlRecord& record);
//	void dataRecordChanged(const QSqlRecord& record);

private:
	QString m_listQuery;
	QString m_listField;
	bool m_sortedListField;
	QStringList m_infoFields;
	QStringList m_infoHeads;
	QString m_keyField;
	QString m_dataQuery;
	QString m_dataField;
	QString m_appendPluginName;
	Validator * m_validator;
	QCompleter * m_completer;
	InfoView * m_infoView;
	SQL::Query * m_lq;
	SQL::Query * m_dq;
	bool m_validateInput;
	bool m_handleFocus;
};

#endif
